//Initialize the values of white mode to default runtime
void loadWhiteModeDefault() {
  values[0] = 5000; //5000K
  values[1] = MAX_BRIGHTNESS / 2; //Half power
  values[2] = 0;
  adjustingCatergory = 0;
  setColorTemperature(5000, MAX_BRIGHTNESS / 2);
  //Set ctrl LED to low brightness white
  setControlLEDColor(MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS, MAX_CTRLBRIGHTNESS);
}

//Set color temperature set the LED RGB Values based on given temperature K value and brigheness level (0 - 255)
void setColorTemperature(int temperatureK, int brightnessLevel) {
  // Check if brightness is within the valid range [0, 1]
  if (brightnessLevel > MAX_BRIGHTNESS) {
    brightnessLevel = MAX_BRIGHTNESS;
  } else if (brightnessLevel < 0) {
    brightnessLevel = 0;
  }

  //Normalize the brightness value
  double brightness = float(brightnessLevel) / 255.0;
  double normalizedTemperature = (log(temperatureK) - 8.79) / 1.8;

  int red = 0;
  int green = 0;
  int blue = 0;

  normalizedTemperature = constrain(normalizedTemperature, -1.0, 1.0);
  //Handle Red and Green channels
  if (normalizedTemperature <= 0) {
    red = 255;
    green = int(255 * (-0.402 * normalizedTemperature * normalizedTemperature * normalizedTemperature - 0.211 * normalizedTemperature * normalizedTemperature + 1.09 * normalizedTemperature + 0.958));
  } else {
    red = int(255 * (-0.990 * normalizedTemperature * normalizedTemperature * normalizedTemperature + 2.34 * normalizedTemperature * normalizedTemperature - 1.99 * normalizedTemperature + 0.970));
    green = int(255 * (-0.542 * normalizedTemperature * normalizedTemperature * normalizedTemperature + 1.37 * normalizedTemperature * normalizedTemperature - 1.28 * normalizedTemperature + 0.941));
  }

  //Blue channel
  if (normalizedTemperature <= -0.7) {
    blue = int(0);
  } else if (normalizedTemperature <= 0.0) {
    blue = int(255 * (0.0117 * normalizedTemperature * normalizedTemperature * normalizedTemperature + 2.05 * normalizedTemperature * normalizedTemperature + 2.85 * normalizedTemperature + 1.00));
  } else {
    blue = int(255 * (1.0));
  }

  //Scale it down to target brightness level
  red = red * brightness;
  green = green * brightness;
  blue = blue * brightness;

  //Make sure non of the values goes over 255
  if (red > 255){
    red = 255;
  }
  //Green usually wont go over 255
  if (blue > 255){
    blue = 255;
  }
  Serial.println(red);
  Serial.println(green);
  Serial.println(blue);

  //Set all the LEDs to desired color temperature
  setLightColor(red, green, blue);
}
